package com.bulletelc.apollopre.module.main.center.orders;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.framework.MVPBaseActivity;
import com.bulletelc.apollopre.module.main.center.orders.fragment.OrderStateFragment;
import com.bulletelc.apollopre.widget.FoolBar;

import java.util.ArrayList;
import java.util.List;

/**
 * author: lhl
 * date  : 2018/2/2
 * desc  : 我的订单界面，包括所有订单、待支付、待发货、待签收、待评价
 */

public class OrderListActivity extends MVPBaseActivity<OrderListContract.View, OrderListPresenter> implements OrderListContract.View ,
        FoolBar.ClickCallback, ViewPager.OnPageChangeListener {

    // views
    private FoolBar flBar;
    private TabLayout orderTabLayout;
    private ViewPager orderViewPager;

    // data
    private OrderStateFragmentAdapter orderFragmentAdapter;
    private int position; // 打开的Fragment的位置
    private List<Fragment> fragmentList;

    @Override
    public int getLayoutRes() {
        return R.layout.order_list_activity;
    }

    @Override
    public void assignViews() {
        flBar = findView(R.id.flBar);
        flBar.setTitle(this.getResources().getString(R.string.order_my));
        orderTabLayout = findView(R.id.order_list_tablayou);
        orderViewPager = findView(R.id.order_list_vp);
    }

    @Override
    public void setListener() {
        flBar.setClickCallback(this);
        orderViewPager.addOnPageChangeListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            position = intent.getIntExtra(IntentCode.EXTRA_KEY_ORDER_STATE_POSITION, 0);
        }
        initFragment();

    }

    @Override
    protected OrderListPresenter createPresenter() {
        return new OrderListPresenter();
    }

    /**
     * 初始化Fragment
     */
    private void initFragment() {
        fragmentList = new ArrayList<>();
        OrderStateFragment all = new OrderStateFragment(); //所有
        OrderStateFragment unpaid = new OrderStateFragment(); //待付款
        OrderStateFragment unSend = new OrderStateFragment(); //待发货
        OrderStateFragment unSign = new OrderStateFragment(); //待签收
        OrderStateFragment uncomment = new OrderStateFragment(); //待评论

        Bundle bundleAll = new Bundle();
        bundleAll.putInt(IntentCode.EXTRA_KEY_ORDER_STATE,0);
        all.setArguments(bundleAll);

        Bundle bundleUnpaid = new Bundle();
        bundleUnpaid.putInt(IntentCode.EXTRA_KEY_ORDER_STATE,GlobalConfig.ORDER_STATE_UN_PAID);
        unpaid.setArguments(bundleUnpaid);

        Bundle bundleUnSend = new Bundle();
        bundleUnSend.putInt(IntentCode.EXTRA_KEY_ORDER_STATE,GlobalConfig.ORDER_STATE_UN_SEND);
        unSend.setArguments(bundleUnSend);

        Bundle bundleUnSign = new Bundle();
        bundleUnSign.putInt(IntentCode.EXTRA_KEY_ORDER_STATE,GlobalConfig.ORDER_STATE_UN_SIGN);
        unSign.setArguments(bundleUnSign);

        Bundle bundleUnComment = new Bundle();
        bundleUnComment.putInt(IntentCode.EXTRA_KEY_ORDER_STATE,GlobalConfig.ORDER_STATE_COMPLETE);
        bundleUnComment.putInt(IntentCode.EXTRA_KEY_ORDER_COMMENT_STATE,GlobalConfig.ORDER_STATE_UN_COMMENT);
        uncomment.setArguments(bundleUnComment);

        fragmentList.add(all);
        fragmentList.add(unpaid);
        fragmentList.add(unSend);
        fragmentList.add(unSign);
        fragmentList.add(uncomment);

        orderTabLayout.setupWithViewPager(orderViewPager);
        orderFragmentAdapter = new OrderStateFragmentAdapter(getSupportFragmentManager(), fragmentList);
        orderViewPager.setAdapter(orderFragmentAdapter);
        orderViewPager.setCurrentItem(position);
        orderViewPager.setOffscreenPageLimit(5);
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private class OrderStateFragmentAdapter extends FragmentPagerAdapter{
        private List<Fragment> fragmentList;
        private String[] tabTitle = new String[]{"全部", "待支付", "待发货", "待签收", "待评价"};

        public OrderStateFragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return tabTitle.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitle[position];
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //若注释继承父类方法，可防止fragment自动销毁
            super.destroyItem(container, position, object);
        }
    }
}
